
var chooseSceneNo = 0;
var eKnowSceneList = [];
//关联场景弹框开启
function chooseSceneOpen(){
    $.ajax({
        url : '/api/inter/eKnowScene/getEKnowSceneListByRole',
        data : {},
        type : 'POST',
        async : false,
        success : function(result) {
            if(result.code=='SUCCESS'){
                var sceneId = $('#sceneId').val();//当前场景id
                chooseSceneNo = sceneId;
                eKnowSceneList = result.eKnowSceneList;
                var htmlStr = '<div class="layui-bg-gray" style="padding: 16px;max-height:calc(100vh - 90px);overflow: auto;"><div class="layui-row layui-col-space15">';
                if(eKnowSceneList!=null && eKnowSceneList.length>0){
                    for(var i=0;i<eKnowSceneList.length;i++){
                        var isChooseed = false;
                        var id = eKnowSceneList[i].id;
                        if(chooseSceneNo!=null && chooseSceneNo!=0){
                            if(chooseSceneNo==id)isChooseed=true;
                        }
                        var sceneKey = eKnowSceneList[i].sceneKey;
                        htmlStr+='<div class="layui-col-md6" style="cursor: pointer;" onclick="chooseScene(\''+id+'\');">'+
                            '<div id="chooseView'+id+'" class="layui-card choose-view-none '+(isChooseed?'choose-view':'')+'">'+
                            '<div class="layui-card-header"  style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"  title="'+eKnowSceneList[i].sceneName+'">' +eKnowSceneList[i].sceneName+
                            '<div style="float: right;" id="checkbox'+id+'" '+(isChooseed?'':'hidden')+'><i class="layui-icon layui-icon-ok" style="color: #4BB2FF;font-weight: bold;"></i></div>'+
                            '</div>'+
                            '<div class="layui-card-body" style="height: 100px;"><span class="list-span">' +eKnowSceneList[i].sceneDescribe+'</span></div></div></div>';
                    }
                }
                htmlStr+='</div>'+
                    '<div class="bottom-view-open">'+
                    '<div class="layui-btn-container add-btn-view-open">'+
                    '<button type="button" class="layui-btn layui-bg-blue add-btn-open" onclick="yesChooseScene();">确定</button> '+
                    '</div>'+
                    '</div>';
                layer.open({
                    title:$('#selectSceneTxt').text(),
                    type: 1,
                    offset: 'r',
                    anim: 'slideLeft', // 从右往左
                    area: ['500px', '100%'],
                    shade: 0.5,
                    shadeClose: true,
                    closeBtn:0,
                    id: 'ID-demo-layer-direction-r',
                    content:htmlStr
                })
            }else{
                layer.msg('不好意思，系统出现问题了，请稍后重试！', {icon: 2});
            }
        }
    });
}

//弹框选择场景样式
function chooseScene(id){
    chooseSceneNo = id;
    var oldId = $('#sceneId').val();
    if(oldId!=id){
        //旧的取消
        $('#chooseView'+oldId).removeClass('choose-view');
        $('#checkbox'+oldId).hide();
        //添加新选择
        $('#chooseView'+id).addClass('choose-view');
        $('#checkbox'+id).show();
        $('#sceneId').val(id);

    }else {
        //chooseListNo.push({id:id,name:name});
        //$('#chooseView'+id).addClass('choose-view');
        //$('#checkbox'+id).show();
    }
}

//确认
function yesChooseScene() {
    layer.load(2);
    $('#sceneId').val(chooseSceneNo);
    resetSceneSet();
}

//重新设置场景信息
function resetSceneSet(){
    var sceneId = $('#sceneId').val();

    //获取场景信息
    $.ajax({
        url: '/api/inter/eKnowChatSetController/getEKnowSceneSet',
        data: {
            'sceneId':sceneId
        },
        type: 'POST',
        async: true,
        success: function (result) {
            console.log('result',result);
            $('#box').show();
            var eKnowScene = result.eKnowScene;
            var modelList = result.modelList;
            var promptKeyList = result.promptKeyList;
            var knowledgeBaseList = result.knowledgeBaseList;
            var userSetAiName = result.userSetAiName;
            eKnowSceneInfo = eKnowScene;
            //配置中间聊天页面信息
            setChatView(eKnowScene,userSetAiName,modelList);

            layer.closeAll();
        },
        error: function () {}
    });

}


